package com.chenyuxin.ilp.service.user.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyuxin.ilp.mapper.user.UserMapper;
import com.chenyuxin.ilp.mapper.user.UserStatisticsMapper;
import com.chenyuxin.ilp.model.entity.user.User;
import com.chenyuxin.ilp.model.entity.user.UserStatistics;
import com.chenyuxin.ilp.model.pojo.Result;
import com.chenyuxin.ilp.model.vo.user.UserInformationVo;
import com.chenyuxin.ilp.model.vo.user.UserSelectVo;
import com.chenyuxin.ilp.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserStatisticsMapper userStatisticsMapper;

    /**
     * 根据用户名或手机号查询用户信息
     * @param username 用户名
     * @return User对象
     */
    public User selectByUsernameOrPhone(String username){
        LambdaQueryWrapper<User> usernameWrapper = new LambdaQueryWrapper<>();
        usernameWrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(usernameWrapper);
        if(user!=null){
            return user;
        }
        LambdaQueryWrapper<User> phoneWrapper = new LambdaQueryWrapper<>();
        phoneWrapper.eq(User::getPhone, username);
        user = userMapper.selectOne(phoneWrapper);
        return user;
    }


    @Override
    public Result selectUserByPhoneOrName(String param) {
        LambdaQueryWrapper<User> phoneWrapper = new LambdaQueryWrapper<>();
        phoneWrapper.like(User::getPhone,param)
                .eq(User::getStatus,1);
        List<User> phoneUsers = userMapper.selectList(phoneWrapper);
        LambdaQueryWrapper<User> nameWrapper = new LambdaQueryWrapper<>();
        nameWrapper.like(User::getUsername,param)
                .eq(User::getStatus,1);
        List<User> nameUsers = userMapper.selectList(nameWrapper);
        List<UserSelectVo> userSelectList = new ArrayList<>();
        for(User user:phoneUsers){
            UserSelectVo userSelectVo = BeanUtil.copyProperties(user, UserSelectVo.class);
            userSelectVo.setSelect(0);
            userSelectList.add(userSelectVo);
        }
        for(User user:nameUsers){
            UserSelectVo userSelectVo = BeanUtil.copyProperties(user, UserSelectVo.class);
            userSelectVo.setSelect(1);
            userSelectList.add(userSelectVo);
        }
        return Result.ok(userSelectList);
    }

    @Override
    public Result getUserInformation(String userId) {
        User user = userMapper.selectById(userId);
        UserStatistics userStatistics = userStatisticsMapper.selectById(userId);
        UserInformationVo userInformationVo = BeanUtil.copyProperties(user, UserInformationVo.class);
        BeanUtil.copyProperties(userStatistics,userInformationVo);
        return Result.ok(userInformationVo);
    }
}
